package com.cn.longdistancebusstation.registerAndLogin;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.base.BaseActivity;
import com.cn.longdistancebusstation.httpHelper.HttpHelper;
import com.cn.longdistancebusstation.httpapi.HttpService;
import com.cn.longdistancebusstation.model.Result;
import com.google.gson.Gson;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by Administrator on 2018/1/17.
 */

public class ResettingPasswordActivity extends BaseActivity implements View.OnClickListener{
    private EditText mPasswordEditText,mSurePwdEditText;
    private Button btn_reset_accomplish;
    private String mPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resettingpwd);
        getTitleTextView().setText("重置密码");
        Intent intent = getIntent();
        mPhone = intent.getStringExtra("phone");
        initview();

    }

    private void initview() {
        mPasswordEditText=(EditText)findViewById(R.id.reset_pwd);
        mPasswordEditText.setOnClickListener(this);
        mSurePwdEditText=(EditText)findViewById(R.id.reset_sure_pwd);
        btn_reset_accomplish=(Button)findViewById(R.id.reset_btn_pwd);
        btn_reset_accomplish.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.reset_btn_pwd:
                accomplish();
                break;
        }
    }

    private void accomplish() {
        final String password = mPasswordEditText.getText().toString().trim();
        final String sure_pwd = mSurePwdEditText.getText().toString().trim();
        if(password.equals(" ")||sure_pwd.equals("")) {
            Toast.makeText(this, "输入内容不能为空", Toast.LENGTH_SHORT).show();
        }if (!password.equals(sure_pwd)){
            Toast.makeText(this, "您输入的密码不一致，请重新输入！", Toast.LENGTH_SHORT).show();
        }else {
            //重置密码，调接口
            reSettingPassword();
        }
    }

    private void reSettingPassword() {
        showHud();
        String strPassword = mPasswordEditText.getText().toString().trim();
        Retrofit retrofit=new HttpHelper.HttpServiceBuilder().build().getRetrofit();
        HttpService httpService=retrofit.create(HttpService.class);
        Call<Result> call=httpService.resetPassword(mPhone, strPassword);
        call.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                hideHud();
                Result result=response.body();
                Boolean isSuccess=result.getHead().getSuccess();
                if(isSuccess){
                    Toast.makeText(ResettingPasswordActivity.this,"密码修改成功,请重新登陆",Toast.LENGTH_SHORT).show();
                    Intent in=new Intent();
                    in.setAction("exit_current_boundary");
                    sendBroadcast(in);
                    finish();
                }else{
                    Toast.makeText(ResettingPasswordActivity.this,"密码修改失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                hideHud();
                Toast.makeText(ResettingPasswordActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        });

    }
}
